package com.gcit.seniorWish.web.modular.enroll.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gcit.seniorWish.core.base.controller.BaseController;
import com.gcit.seniorWish.core.base.tips.ErrorTip;
import com.gcit.seniorWish.core.base.tips.Tip;
import com.gcit.seniorWish.core.node.SelectNode;
import com.gcit.seniorWish.web.common.constant.factory.PageFactory;
import com.gcit.seniorWish.web.common.persistence.model.EnrollSchool;
import com.gcit.seniorWish.web.core.exception.BizExceptionEnum;
import com.gcit.seniorWish.web.core.log.LogObjectHolder;
import com.gcit.seniorWish.web.modular.enroll.service.IEnrollSchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 报考学校控制器
 *
 * @author fengshuonan
 * @Date 2018-03-16 15:41:45
 */
@Controller
@RequestMapping("/enrollSchool")
public class
EnrollSchoolController extends BaseController {

    private String PREFIX = "/enroll/enrollSchool";

    @Autowired
    private IEnrollSchoolService enrollSchoolService;

    /**
     * 跳转到报考学校首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/enrollSchool.html";
    }

    /**
     * 跳转到添加报考学校
     */
    @RequestMapping("/enrollSchool_add")
    public String enrollSchoolAdd() {
        return PREFIX + "/enrollSchoolAdd.html";
    }

    /**
     * 跳转到修改报考学校
     */
    @RequestMapping("/enrollSchool_update/{enrollSchoolId}")
    public String enrollSchoolUpdate(@PathVariable String enrollSchoolId, Model model) {
        EnrollSchool enrollSchool = enrollSchoolService.selectById(enrollSchoolId);
        model.addAttribute("item",enrollSchool);
        model.addAttribute("batchTypeList",enrollSchoolService.listBatchTypeForSelect2(enrollSchoolId));
        LogObjectHolder.me().set(enrollSchool);
        return PREFIX + "/enrollSchoolInfo.html";
    }

    /**
     * 获取报考学校列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        Page<EnrollSchool> page=new PageFactory<EnrollSchool>().defaultPage();
        EntityWrapper<EnrollSchool> ew = new EntityWrapper<>();
        if(StrUtil.isNotBlank(condition)){
            ew.like("school_name",condition).or().like("school_code",condition);
        }
        page.setRecords(enrollSchoolService.listEnrollSchool(page,ew));
        return this.packForBT(page);
    }

    /**
     * 新增报考学校
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(EnrollSchool enrollSchool) {
        enrollSchoolService.addEnrollSchool(enrollSchool);
        return SUCCESS_TIP;
    }

    /**
     * 删除报考学校
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam String enrollSchoolId) {
        enrollSchoolService.deleteEnrollSchool(enrollSchoolId);
        return SUCCESS_TIP;
    }

    /**
     * 修改报考学校
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(EnrollSchool enrollSchool) {
        enrollSchoolService.updateEnrollSchool(enrollSchool);
        return SUCCESS_TIP;
    }


    /**
    * 查询报考类型列表
    * @author gaozhichang
    * @date 2018-03-19 下午 4:01
    */
    @RequestMapping(value="/listBatchTypeForSelect2")
    @ResponseBody
    public Object listBatchTypeForSelect2(){
        Page<SelectNode> page = new Page();
        page.setRecords(enrollSchoolService.listBatchTypeForSelect2(null));
        return packForSelect2(page);
    }

    /**
    * 根据批次id查询报考学校列表
    * @param batchType 批次类型
    * @author gaozhichang
    * @date 2018-03-20 上午 11:24
    */
    @RequestMapping(value="/listEnrollSchoolForSelect2")
    @ResponseBody
    public Object listEnrollSchoolForSelect2(Integer batchType,String enrollSchoolName){
    	Page<Map<String,Object>> page =new PageFactory<Map<String,Object>>().defaultPage();
	    page.setRecords(enrollSchoolService.listEnrollSchoolForSelect2(page,batchType,null,enrollSchoolName));
	    return packForSelect2(page);
    }
    /**
	 * 根据模板导入报考学校信息
	 * 
	 * @param
	 * @author hutao
	 * @date 2018-03-22 下午 1:56
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/importEnrollSchool")
	@ResponseBody
	public Object importEnrollSchool(@RequestPart("file") MultipartFile excelFile) {
		try {
			String msg = enrollSchoolService.importEnrollSchool(excelFile.getInputStream());
			if (StrUtil.isBlank(msg)) {
				msg = "导入成功";
			}
			// 返回导入结果
			return new Tip(200, msg);
		} catch (IOException e) {
			e.printStackTrace();
			return new ErrorTip(BizExceptionEnum.SERVER_ERROR.getCode(), BizExceptionEnum.SERVER_ERROR.getMessage());
		}
	}
	
	 /**
		 * 根据模板导入报考专业信息
		 * 
		 * @param
		 * @author hutao
		 * @date 2018-03-23  10:56
		 */
		@RequestMapping(method = RequestMethod.POST, path = "/importMajor")
		@ResponseBody
		public Object importMajor(@RequestPart("file") MultipartFile excelFile) {
			try {
				String msg = enrollSchoolService.importMajor(excelFile.getInputStream());
				if (StrUtil.isBlank(msg)) {
					msg = "导入成功";
				}
				// 返回导入结果
				return new Tip(200, msg);
			} catch (IOException e) {
				e.printStackTrace();
				return new ErrorTip(BizExceptionEnum.SERVER_ERROR.getCode(), BizExceptionEnum.SERVER_ERROR.getMessage());
			}
		}
}
